require "logUtil"
require "util"
require "config"

FuDi = {};

function FuDi.ZiChan_JingYing()
    x, y = findMultiColorInRegionFuzzy(0xb96521, "-41|-28|0xd28645,152|-26|0xcf8240,119|12|0xce8029,77|30|0xedbc3c,0|24|0xd1872a,-69|3|0xd9c4a6,63|-45|0xdac6a8,58|51|0xd3bfa1,186|0|0xdfccaf", 95, 78, 1278, 1007, 1421, 0, 0)
    if x > -1 then
        shouzhi.click(x, y)
        logger.debug("点击:资产_经营")
        return true;
    end
end


function FuDi.ZhiChan_ui()
    x, y = findMultiColorInRegionFuzzy(0xfffbb6, "-93|-12|0xfffcee,-110|0|0xfff6d4,-100|15|0xffeeaf,-64|10|0xfff1be,-53|7|0xfff3c6,-52|27|0xffe792,-67|33|0xffe68a,-108|-1|0xfff7d6,-326|5|0x3e2c28,-461|12|0xe79c79,-459|-3|0xf4c999,-408|-20|0xfcedbd,-355|-19|0xfcedbd", 95, 325, 369, 1078, 556, 0, 0)
    if x > -1 then
        logger.debug("资产.界面")
        return true;
    end

end

function FuDi.ZhiChan_GuanBi()
    x, y = findMultiColorInRegionFuzzy(0xfffbb6, "-93|-12|0xfffcee,-110|0|0xfff6d4,-100|15|0xffeeaf,-64|10|0xfff1be,-53|7|0xfff3c6,-52|27|0xffe792,-67|33|0xffe68a,-108|-1|0xfff7d6,-326|5|0x3e2c28,-461|12|0xe79c79,-459|-3|0xf4c999,-408|-20|0xfcedbd,-355|-19|0xfcedbd", 95, 325, 369, 1078, 556, 0, 0)
    if x > -1 then
        shouzhi.click(x, y)
        logger.debug("资产.关闭")
        return true;
    end

end


-- 来福
function FuDi.LaiFu()
    x, y = findMultiColorInRegionFuzzy(0xc3cdde, "-21|-112|0xf7c5a2,-18|-139|0x252427,-27|-188|0xf3cc9a,11|-196|0xf3cc9a,-86|-196|0xf3cc9a,54|10|0xced7e6,-118|-42|0x090909,-115|1|0x10141b,18|93|0x0c0c0c", 95, 3, 950, 1078, 1657, 0, 0)
    if x > -1 then
        shouzhi.click(x, y)
        logger.debug("点击:来福")
        return true;
    end
end

--章师爷
function FuDi.zhangShiYe()
    x, y = findMultiColorInRegionFuzzy(0x4e4f4e, "-34|43|0x1c1d1c,-21|77|0xc1896c,146|-9|0xd6c1ab,6|-100|0xe6bca8,-6|-148|0x161819,-15|-202|0xf3cc9a,25|-209|0xf3cc9a,-69|-208|0xf3cc9a", 95, 3, 950, 1078, 1657, 0, 0)
    if x > -1 then
        shouzhi.click(x, y)
        logger.debug("点击:章师爷")
        return true;
    end
end

-- 先生
function FuDi.xianSheng()
    x, y = findMultiColorInRegionFuzzy(0x1f344e, "24|-12|0x1d324c,-26|-120|0xf2d1ac,-65|14|0x131518,115|-3|0xd6c1ab,-58|74|0x090a0c,-44|143|0x0e1012", 95, 691, 1072, 1074, 1664, 0, 0)

    if x > -1 then
        shouzhi.click(x, y)
        logger.debug("点击:先生")
        return true;
    end
end

-- 寻访
function FuDi.xunFang()
    x, y = findMultiColorInRegionFuzzy(0xd0b386, "-125|-148|0xd6c1aa,-70|1|0xce5439,-25|9|0xe19774,41|7|0xec7f59,83|7|0xda8e4d,4|5|0xdcc99e,16|-109|0x7b523d,-29|-108|0x7d4e3e", 95, 43, 633, 1078, 1064, 0, 0)
    if x > -1 then
        shouzhi.click(x, y)
        logger.debug("点击:寻访")
        return true;
    end
end


-- 内宅
function FuDi.neiZhai()
    x, y = findMultiColorInRegionFuzzy(0x482222,"44|35|0x4a2123,214|1|0xd6c1ab,217|-99|0xd6c1ab,8|-157|0xa8d9e0,-26|-3|0x090707,-74|-1|0x0c0b0d,89|13|0x170e0f", 95, -1, 509, 500, 976, 0, 0)
    if x > -1 then
        shouzhi.click(x, y)
        logger.debug("点击:内宅")
        return true;
    end
end


FuDi.ZhouZhe = {};

function FuDi.ZhouZhe.Ui()
    x, y = findMultiColorInRegionFuzzy(0xd35600, "-4|-28|0xfdd170,16|-5|0xf7bd5a,0|19|0xedb753,-141|-52|0x703f3f,-864|51|0xfedeb2,-881|320|0xfedeb2,-888|191|0x401b16,-874|221|0x401b16,-880|100|0x401b16,-876|531|0x750201,-872|469|0x750201", 95, 25, 408, 1054, 1330, 0, 0)
    if x > -1 then
        logger.debug("奏折界面")
        return true;
    end
end

function FuDi.ZhouZhe.close()
    x, y = findMultiColorInRegionFuzzy(0xd35600, "-4|-28|0xfdd170,16|-5|0xf7bd5a,0|19|0xedb753,-141|-52|0x703f3f,-864|51|0xfedeb2,-881|320|0xfedeb2,-888|191|0x401b16,-874|221|0x401b16,-880|100|0x401b16,-876|531|0x750201,-872|469|0x750201", 95, 25, 408, 1054, 1330, 0, 0)
    if x > -1 then
        shouzhi.click(x, y)
        logger.debug("点击:奏折关闭")
        return true;
    end
end


function FuDi.ZhouZhe.meiYou()
    x, y = findMultiColorInRegionFuzzy(0x750201, "-3|45|0xfedeb2,202|30|0xbfb2b8,211|351|0x410c00,171|392|0x100500,165|278|0x0b0300,130|334|0xaf9787,146|199|0xd7cdd0", 95, 38, 940, 590, 1520, 0, 0)
    if x > -1 then
        shouzhi.click(x, y)
        logger.debug("没有政务")
        return true;
    end
end

function FuDi.ZhouZhe.zhengWu()
    shouzhi.click(647, 1430)
    logger.debug("处理政务")
end





local xianSheng = {}

function xianSheng.wanCheng()
    x, y = findMultiColorInRegionFuzzy(0x170c00, "106|-34|0x170c00,226|3|0x160c00,58|-100|0xa7734c,104|-100|0x9f6b44,155|-100|0xa16d46,64|-189|0x00ff00,108|-185|0x00ff00,157|-178|0x00fe00,155|-197|0x00ff00", 95, 18, 331, 1063, 1879, 0, 0)
    if x > -1 then
        shouzhi.click(x, y)
        logger.debug("学院.读书完成")
        return true;
    end

end

function xianSheng.paiQianXueXi()

    x, y = findMultiColorInRegionFuzzy(0x170c00, "108|-36|0x170c00,222|-9|0x160c00,119|-192|0xfdfbba,54|-103|0x9f6c45,107|-103|0xa06d46,157|-103|0xa06d46,210|-178|0xfffdbc,20|-194|0xfcfaba,-6|-188|0xfbf9b9", 95, 18, 331, 1063, 1879, 0, 0)
    if x > -1 then
        shouzhi.click(x, y)
        logger.debug("学院.派遣学习")
        return true;
    end
end

function xianSheng.paiQian()
    x, y = findMultiColorInRegionFuzzy(0x365e92, "-24|-44|0x4a72a0,170|-46|0x4c75a3,146|-3|0x33588b,-11|3|0x375f93,-83|-6|0x4e3f3b,42|-70|0x4e3f3b,57|40|0x4e3f3b,205|-16|0x493b37", 95, 660, 415, 1007, 1686, 0, 0)
    if x > -1 then
        shouzhi.click(x, y)
        logger.debug("学院.派遣")
        return true;
    end
end


function xianSheng.ui()

    x, y = findMultiColorInRegionFuzzy(0xd35600, "-22|0|0xf5ba59,-1|-21|0xfcce6f,-1|16|0xeeb753,21|-4|0xf5c160,-419|-39|0x391412,-536|-37|0x391412,-472|-88|0x315565,-540|-86|0x365c59,-506|-46|0xfbe7b8,-504|-35|0xf3c89e,-455|-40|0xf7d7aa,-447|-38|0xf5d2a5,-440|-16|0xe69773,-513|-30|0xeeb991", 95, 335, 17, 1074, 246, 0, 0)
    if x > -1 then

        logger.debug("学院.界面")
        return true;
    end
end

function xianSheng.close()

    x, y = findMultiColorInRegionFuzzy(0xd35600, "-22|0|0xf5ba59,-1|-21|0xfcce6f,-1|16|0xeeb753,21|-4|0xf5c160,-419|-39|0x391412,-536|-37|0x391412,-472|-88|0x315565,-540|-86|0x365c59,-506|-46|0xfbe7b8,-504|-35|0xf3c89e,-455|-40|0xf7d7aa,-447|-38|0xf5d2a5,-440|-16|0xe69773,-513|-30|0xeeb991", 95, 335, 17, 1074, 246, 0, 0)
    if x > -1 then
        shouzhi.click(x, y)
        logger.debug("学院.界面关闭")
        return true;
    end
end




neiZhai = {}

function neiZhai.ui()
    x, y = findMultiColorInRegionFuzzy(0x5d1800,"-20|4|0x5d1800,-20|23|0x5d1800,-10|22|0x5d1800,2|22|0x5d1800,-13|149|0xcf1c21,-13|149|0xcf1c21,-17|45|0x5d1800,-10|54|0x5d1800,-68|-45|0xfce570,30|63|0xd0672a,-31|-20|0xd77a2e", 95, 725, 402, 963, 715, 0, 0)

    if x > -1 then
        logger.debug("内宅.界面")
        return true;
    end
end

function neiZhai.close()
    x, y = findMultiColorInRegionFuzzy(0x5d1800,"-20|4|0x5d1800,-20|23|0x5d1800,-10|22|0x5d1800,2|22|0x5d1800,-13|149|0xcf1c21,-13|149|0xcf1c21,-17|45|0x5d1800,-10|54|0x5d1800,-68|-45|0xfce570,30|63|0xd0672a,-31|-20|0xd77a2e", 95, 725, 402, 963, 715, 0, 0)

    if x > -1 then
        shouzhi.click(x, y)
        logger.debug("内宅.关闭")
        return true;
    end
end


function neiZhai.jiaRen()
    x, y = findMultiColorInRegionFuzzy(0xe5e5de, "-134|-9|0xd6c1ab,-14|-57|0xf3dac2,-37|-148|0x0b090a,-11|-199|0xf3cc9a,-56|-209|0xf3cc9a,-22|221|0xc2cc79,3|152|0xe5e5dd", 95, 281, 929, 993, 1645, 0, 0)
    if x > -1 then
        shouzhi.click(x, y)
        logger.debug("内宅.佳人")
        return true;
    end
end

function neiZhai.jiaRen_ui()
    x, y = findMultiColorInRegionFuzzy(0xd45600, "-25|-4|0xf8be59,2|-25|0xfdd170,27|0|0xf4b958,-381|10|0x060506,-670|-30|0x3b0c00,-752|-23|0x5f2622,-823|30|0x2f1e17,-891|16|0x2f1e17,-887|9|0x2f1e17,-872|29|0x2f1e17,-825|29|0x2f1e17,-825|39|0x2f1e17", 95, 82, 13, 1079, 188, 0, 0)
    if x > -1 then
        logger.debug("内宅.佳人.界面")
        return true;
    end
end


function neiZhai.ziSi()

    x, y = findMultiColorInRegionFuzzy(0x8bb5b8, "-19|-93|0xe7e4c9,-30|-123|0x050302,110|-53|0xd6c1ab,-18|-197|0xf3cc9a,27|-207|0xf0c998,76|-205|0xf3cc9a", 95, 647, 1057, 989, 1582, 0, 0)
    if x > -1 then
        shouzhi.click(x, y)
        logger.debug("内宅.子嗣")
        return true;
    end
end



function neiZhai.ziSi_ui()

    x, y = findMultiColorInRegionFuzzy(0xd35600, "-27|-1|0xf6bc5c,-7|-28|0xfdd170,-730|-10|0x6a2c22,-659|-25|0x3b0c00,-757|27|0x2f1e17,-778|-28|0x5d2521", 95, 20, 7, 1075, 288, 0, 0)
    if x > -1 then

        logger.debug("子嗣.界面")
        return true;
    end
end

function neiZhai.ziSi_close()
    x, y = findMultiColorInRegionFuzzy(0xd35600, "-27|-1|0xf6bc5c,-7|-28|0xfdd170,-730|-10|0x6a2c22,-659|-25|0x3b0c00,-757|27|0x2f1e17,-778|-28|0x5d2521", 95, 20, 7, 1075, 288, 0, 0)
    if x > -1 then
        shouzhi.click(x, y)
        logger.debug("子嗣.界面关闭")
        return true;
    end

end

function neiZhai.ziSi_find(tempX)
    if tempX == nil then
        tempX = 75;
    else
        tempX = fenBianLv.runToDev_x(tempX + 100)
    end

    x, y = findMultiColorInRegionFuzzy(0x6e5a40, "131|-5|0x6e5a40,56|251|0xb5a08d,-12|9|0xb5a08d,146|-4|0xb5a08d", 95, 75, 1352, 1057, 1889, 0, 0)
    if x > -1 then
        shouzhi.click(x, y)
        logger.debug("子嗣.切换子嗣")
        return x, true;
    end
end

function neiZhai.ziSi_peiYang()
    x, y = findMultiColorInRegionFuzzy(0xc87926, "135|-5|0xce8029,-28|-53|0xd48947,174|-54|0xdb9b57,-10|-4|0xc37026,157|-6|0xc97627", 95, 362, 1236, 742, 1392, 0, 0)
    if x > -1 then
        shouzhi.click(x, y)
        logger.debug("子嗣.培养")
        return true;
    end
end
function neiZhai.ziSi_quMing()
    x, y = findMultiColorInRegionFuzzy(0x972516,"-19|-42|0x962b1e,163|-42|0x912a1d,154|14|0xa53119,222|-10|0xd9c7b1,70|-61|0xdac8b2,-68|-21|0xd4c2ac,94|-7|0xffffff,61|-6|0xffffff,80|6|0xc4421d,35|-9|0x962416,80|-23|0x891f14,112|-12|0x982716,69|-5|0xffffff", 95, 389, 1262, 768, 1384, 0, 0)

    if x > -1 then
        shouzhi.click(x, y)
        logger.debug("子嗣.取名")
        return true;
    end
end



function neiZhai.ziSi_quMingQueRen()
    x, y = findMultiColorInRegionFuzzy(0xeab83a, "-57|1|0xe6ae37,-353|-1|0xe9b439,-401|4|0xebb93b,-135|-59|0xce813e,79|-59|0xce813e,83|-383|0x3f1a1a,183|-385|0x3c1a1a,10|-374|0x642826,-161|-405|0x470404,-221|-365|0x470404", 95, 261, 640, 1027, 1142, 0, 0)
    if x > -1 then
        shouzhi.click(x, y)
        logger.debug("子嗣.取名确认")
        return true;
    end
end

function neiZhai.ziSi_huoLiDanQuXiao()

    x, y = findMultiColorInRegionFuzzy(0x386195,"111|-4|0x365d91,-12|-55|0x5079a8,135|-55|0x4e78a6,-433|-14|0x345a8c,-322|-8|0x345b8d,-454|-56|0x517aaa,-317|-51|0x4972a0,118|82|0xffd093,-304|81|0xffd093,321|-28|0xffd093", 95, 42, 704, 1071, 1274, 0, 0)
    if x > -1 then
        shouzhi.click(x, y)
        logger.debug("子嗣.取消活力丹")
    end
end


function neiZhai.ziSi_run()
    neiZhai.ziSi()

    local flag = neiZhai.ziSi_ui()
    if not (flag) then
        return
    end
    local count = 0;

    local tempX = nil;
    while count < 2 do
        flag = true
        while flag do
            local funcArr = { neiZhai.ziSi_quMing, neiZhai.ziSi_quMingQueRen, neiZhai.ziSi_peiYang,ziSi_huoLiDanQuXiao}
            for i, func in ipairs(funcArr) do
                flag = func()
                if flag then
                    count = 0
                end
                mSleep(50)
            end
            mSleep(500)
        end

        -- 切换子嗣
        local  tempX1, tempFlag = neiZhai.ziSi_find(tempX)
        if tempX1 ~=nil and tempX1>-1 then
            tempX= tempX1
        end

        count = count + 1
    end

    neiZhai.ziSi_close()
    return true;
end

function neiZhai.run ()

    FuDi.neiZhai()
    mSleep(1000)

    local flag = neiZhai.ui()
    if not (flag) then
        return
    end

    neiZhai.jiaren_run();
    neiZhai.ziSi_run();
    neiZhai.close()
    logger.debug("完成内宅")
end


function neiZhai.jiaRen_close()
    x, y = findMultiColorInRegionFuzzy(0xd45600, "-25|-4|0xf8be59,2|-25|0xfdd170,27|0|0xf4b958,-381|10|0x060506,-670|-30|0x3b0c00,-752|-23|0x5f2622,-823|30|0x2f1e17,-891|16|0x2f1e17,-887|9|0x2f1e17,-872|29|0x2f1e17,-825|29|0x2f1e17,-825|39|0x2f1e17", 95, 82, 13, 1079, 188, 0, 0)
    if x > -1 then
        shouzhi.click(x, y)
        logger.debug("内宅.佳人.界面关闭")
        return true;
    end
end


function neiZhai.jiaRen_deZi()
    x, y = findMultiColorInRegionFuzzy(0xca7c28,"133|-2|0xc97a27,-47|-46|0xd28645,144|-48|0xd48947,122|-143|0xf9c586,-322|-5|0xcf8329,-217|-123|0xffecb6,-298|-118|0xffefc3,-362|-136|0xffdc95,-428|-141|0xfbcc8a", 95, 120, 1366, 924, 1596, 0, 0)

    if x > -1 then
        shouzhi.click(x, y)
        logger.debug("内宅.佳人.喜得贵子")
        return true;
    end
end



function neiZhai.jiaRen_chuanHuan()
    x, y = findMultiColorInRegionFuzzy(0xb56020, "-2|-28|0xcf8342,202|-26|0xcd7f3d,192|4|0xbe6c23,249|40|0xe8cdb0,252|-27|0xefd7bb,-51|-19|0xf0d9ba,74|-53|0xefd7bb,87|62|0xeacfb2", 95, 621, 1682, 1079, 1908, 0, 0)
    if x > -1 then
        shouzhi.click(x, y)
        logger.debug("佳人.随机传唤")
        return true;
    end
end

function neiZhai.jiaRen_chuanHuanTuPian()
    shouzhi.click(8, 1446)
end

function neiZhai.jiaRen_huiFuJingLi()
    x, y = findMultiColorInRegionFuzzy(0x942a1d, "11|27|0x831d14,159|-6|0x962e21,178|25|0x801a12,-44|23|0xeed9b9,67|-29|0xefd7bb,223|22|0xf2d7ba,109|72|0xdfc7ab", 95, 743, 1780, 1066, 1911, 0, 0)
    if x > -1 then
        logger.debug("佳人.精力用完了")
        return true;
    end
end


xunFang = {}

function xunFang.ui()

    x, y = findColor({ 466, 40, 1074, 233 },
    "1014|166|0xd35600,993|168|0xf5ba5c,1013|141|0xfdd170,1014|192|0xecb653,520|120|0xfbe7b8,514|128|0xf5d3a6,510|142|0xebab85,553|128|0xf5d3a6,486|135|0x391412,594|134|0x391412",
    95, 0, 0, 0)
    if x > -1 then
        logger.debug("寻访.界面")
        return true;
    end

end

function xunFang.close()

    x, y = findColor({ 466, 40, 1074, 233 },
    "1014|166|0xd35600,993|168|0xf5ba5c,1013|141|0xfdd170,1014|192|0xecb653,520|120|0xfbe7b8,514|128|0xf5d3a6,510|142|0xebab85,553|128|0xf5d3a6,486|135|0x391412,594|134|0x391412",
    95, 0, 0, 0)
    if x > -1 then
        shouzhi.click(x, y)
        logger.debug("寻访.界面关闭")
        return true;
    end

end


function xunFang.xunFang()

    x, y = findMultiColorInRegionFuzzy(0xca7b28, "-14|-14|0xba6621,129|-39|0xca7c3a,110|-9|0xcb7c28,24|-65|0x000000,-84|-12|0x000000,10|38|0x000000", 95, 716, 1685, 1051, 1861, 0, 0)
    if x > -1 then
        shouzhi.click(x, y)
        logger.debug("寻访.寻访")
        return true;

    end

end


function xunFang.tiaoGuo()

    shouzhi.click( 1051, 1712)

end

function xunFang.huiFuTiLi()
    x, y = findMultiColorInRegionFuzzy(0x962d20, "-4|24|0x7f1a12,168|2|0x8f291c,165|27|0x7f1a12,-43|23|0x000000,63|-24|0x000000,65|73|0x000000,207|27|0x000000", 95, 765, 1742, 1057, 1870, 0, 0)
    if x > -1 then
        logger.debug("寻访.没有体力了")
        return true;
    end
end


function xunFang.run()
    FuDi.xunFang();
    local flag = xunFang.ui()
    if not (flag) then
        return
    end

    local funcArr = { xunFang.xunFang, xunFang.tiaoGuo }
    while flag do
        for i, func in ipairs(funcArr) do
            func()
            mSleep(100)
        end
        mSleep(5000)
        flag = xunFang.huiFuTiLi()
        flag = not (flag)
        mSleep(200)
        wangLuoLianJieShiBai()
    end
    xunFang.close()
end



function FuDi.ZhouZhe.run()
    FuDi.zhangShiYe();
    local flag = FuDi.ZhouZhe.Ui()
    if not (flag) then
        return
    end

    while flag do

        FuDi.ZhouZhe.zhengWu()
        flag = FuDi.ZhouZhe.meiYou()
        flag = not (flag)
        mSleep(5000)
    end

    FuDi.ZhouZhe.close()
end


function xianSheng.run()

    FuDi.xianSheng();
    local flag = xianSheng.ui()
    if not (flag) then
        return
    end

    mSleep(500)
    local funcArr = { xianSheng.paiQianXueXi, xianSheng.paiQian, xianSheng.wanCheng }
    while flag do
        for i, func in ipairs(funcArr) do
            flag = func()
            if flag then
                break
            end
            mSleep(100)
        end
        mSleep(2000)
    end

    xianSheng.close()
end


function neiZhai.jiaren_run()
    neiZhai.jiaRen()
    local flag = neiZhai.jiaRen_ui()
    if not (flag) then
        return
    end

    local funcArr = { neiZhai.jiaRen_chuanHuan, neiZhai.jiaRen_chuanHuanTuPian, neiZhai.jiaRen_deZi }

    while flag do
        for i, func in ipairs(funcArr) do
            func()
            mSleep(100)
        end

        flag = neiZhai.jiaRen_huiFuJingLi()
        flag = not (flag)
        mSleep(1000)
    end

    neiZhai.jiaRen_close()
return true

end


function FuDi.ZhiChan_run()
    FuDi.LaiFu()
    local flag = FuDi.ZhiChan_ui()
    if not (flag) then
        return
    end

    while flag do
        flag = FuDi.ZiChan_JingYing()
        mSleep(500)
    end
    mSleep(2000)
    FuDi.ZhiChan_GuanBi()
end





function FuDi.onlyZiChanRun()
    while true do
        FuDi.ZiChan_JingYing()
        mSleep(1000)
    end
end



function FuDi.run()

    logger.debug("府邸功能运行")

    shouzhi.move(195, 920, 898, 938)

    local funcArr = {}

    for i, func in ipairs(funcArr) do
        func()
    end


    for i = 1, 2 do
        local funcArr = { FuDi.ZhiChan_run, FuDi.ZhouZhe.run, xianSheng.run, xunFang.run,neiZhai.run }
        for i, func in ipairs(funcArr) do
            func()
        end

        logger.debug("移动界面")
        shouzhi.move(898, 938, 195, 920)
    end
    -- 资产


end


